const util = require('../../../utils/util.js');
const api = require('../../../config/api.js');
var app = getApp();
Page({
  data: {
    dmvs: [],
    param: {},
    endFlag: false,
    loadingFlag: false,
    regions: [],
    regionId: 0,
    showType: 2,
    navbar_bgcolor: wx.getStorageSync('navbar_bgcolor'),
    totalPages: 1
  },
  onLoad: function () {
    var that = this;
    that.getRegions();
    that.getDmvs();
  },

  getRegions: function () {
    var that = this;
    util.request(api.RegionList).then(function (res) {
      if (res.errno === 0) {
        that.setData({
          regions: res.data.list
        });
      }

    });
  },

  tabClick: function (e) {
    this.setData({
      regionId: e.currentTarget.id,
      endFlag: false
    });
    this.getDmvs();
  },

  getDmvs: function () {
    var that = this;

    util.request(api.SchoolList, {
      regionId: that.data.regionId,
      starRating: 0,
      type: that.data.showType,
      // page: that.data.page,
      // limit: that.data.limit
    }).then(function (res) {
      if (res.errno === 0) {
        that.setData({
          dmvs: res.data.list,
          totalPages: res.data.pages
        });
      }
    });
  },

  onReachBottom() {
    if (this.data.totalPages > this.data.page) {
      this.setData({
        page: this.data.page + 1
      });
      this.getDmvs();
    } else {
      // wx.showToast({
      //   title: '没有更多数据了',
      //   icon: 'none',
      //   duration: 2000
      // });
      this.setData({
        endFlag: true
      });
      return false;
    }
  },

  //事件处理函数
  bindViewTap: function (e) {
    var param = e.currentTarget.dataset;
    var id = param["id"];
    //console.log("id:::::::"+id);
    wx.navigateTo({
      url: '../detail/detail?id=' + id
    })
  },
  toNav: function (e) {
    var param = e.currentTarget.dataset;
    var latitude = param['latitude'];
    var longitude = param['longitude'];
    var name = param['name'];
    var address = param['address'];

    wx.openLocation({//?使用微信内置地图查看位置。
      latitude: Number(latitude),//要去地点的纬度
      longitude: Number(longitude),///要去地点的经度-地址
      name: name,//
      address: address
    })

    
    //console.log(latitude,longitude)
    /*wx.getLocation({//获取当前经纬度
      type: 'wgs84', //返回可以用于wx.openLocation的经纬度，官方提示bug: iOS 6.3.30 type 参数不生效，只会返回 wgs84 类型的坐标信息
      success: function (res) {
        wx.openLocation({//?使用微信内置地图查看位置。
          latitude: Number(latitude),//要去地点的纬度
          longitude: Number(longitude),///要去地点的经度-地址
          name: name,//
          address: address
        })
      }
    })*/
  }
})